﻿//-----------------------------------------------------------------------
// <copyright file="Rotation.cs" company="FCIS">
//     Company copyright tag.
// </copyright>
//-----------------------------------------------------------------------

namespace IPPackage.ImageOperation
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Text;

    /// <summary>
    /// rotation operation in image
    /// </summary>
    public class Rotation : Open
    {
        /// <summary>
        /// Makes the rotate of image.
        /// </summary>
        /// <param name="seta">The angle of rotate.</param>
        /// <returns>the new image</returns>
        public Bitmap MakeRotate(double seta)
        {
            Color[,] newBuffer = new Color[BufferOfImage.GetLength(0), BufferOfImage.GetLength(1)];
            Bitmap new_image = new Bitmap(BufferOfImage.GetLength(0), BufferOfImage.GetLength(1));
            double x;
            double y;
            for (int i = 0; i < BufferOfImage.GetLength(1); i++)
            {
                for (int j = 0; j < BufferOfImage.GetLength(0); j++)
                {
                    x = (j * Math.Cos(seta)) + (i * Math.Sin(seta));
                    y = (j * -1 * Math.Sin(seta)) + (i * Math.Cos(seta));
                    int new_x = (int)x;
                    int new_y = (int)y;
                    if (new_x < BufferOfImage.GetLength(0) && new_y < BufferOfImage.GetLength(1) && new_x >= 0 && new_y >= 0)
                    {
                        newBuffer[new_x, new_y] = BufferOfImage[j, i];
                        new_image.SetPixel(new_x, new_y, newBuffer[new_x, new_y]);
                    }
                }
            }

            return new_image;
        }
    }
}
